const parkingLot = require('./parkingLot')
class ManageParkingLot {
  constructor(load1, load2) {
    this.load1 = load1
    this.parkingLot1 = new parkingLot(load1)
    this.load2 = load2
    this.parkingLot2 = new parkingLot(load2)
    this.carInfo = {}
  }
  // 入库
  warehousing(carId) {
    // 先存入第一个车库
    if(this.parkingLot1.exitNum < this.load1) {
      const [err, pos] = this.parkingLot1.warehousing(carId)
      if(!err) {
        this.carInfo[carId] = {
          pos,
          index: 1
        }
      }
      return [err, pos]
    }
    const [err2, pos2] = this.parkingLot2.warehousing(carId)
    if(!err2) {
      this.carInfo[carId] = {
        pos: pos2,
        index: 2
      }
    }
    return [err2, pos2]
  }
  // 出库
  exWarehouse(carId) {
    const [errId, pos, index] = this.getCarInfoById(carId)
    if(errId) {
      return [true]
    }
    this['parkingLot' + index].exWarehouse({pos, carId})
    delete this.carInfo[carId]
    return [null]
  }
  // 获取 carId 对应的信息
  getCarInfoById(carId) {
    if(this.carInfo[carId]) {
      return [null, this.carInfo[carId].pos, this.carInfo[carId].index]
    }
    return [true]
  }
}

module.exports = {ManageParkingLot}